﻿using System.Text.RegularExpressions;

namespace Validator
{
    public class PatternValidator : BaseValidator
    {
        private string _regexPattern { get; set; }

        public PatternValidator(Pattern _pattern, string message, string _customPattern = "", string _friendlyName = "")
            : base(message, _friendlyName)
        {
            if (_pattern == Pattern.Custom)
                _regexPattern = _customPattern;
            else
                _regexPattern = _pattern.GetDescription();
        }

        public override bool IsValid(object item, bool ValidateEmptyString = false)
        {
            if (!string.IsNullOrEmpty((string)item))
                return Regex.IsMatch((string)item, _regexPattern);
            else if (ValidateEmptyString)
                return false;
            else
                return true;
        }
    }


}
